package org.hugh.quotation;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONPath;
import com.alibaba.fastjson2.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.hugh.common.RequestMapping;
import org.hugh.http.HttpInvoker;
import org.hugh.model.QuotationInfoEntity;
import org.hugh.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 行情同步
 */
@Component
@Slf4j
public class QuotationSync {

    @Autowired
    private HttpInvoker httpInvoker;
    @Autowired
    private RedisUtil redisUtil;

    /**
     * 定时获取配置证券的行情信息
     */
    @Scheduled(cron = "0/30 * 9-15 * * ?")
    public void syncQuotation() {
        String data = httpInvoker.get(RequestMapping.QUOTATION_ALL.getUrl(), String.class);

        Map<String, QuotationInfoEntity> map = analysisData(data);
        log.info("全量行情同步-查询到行情数据量 {}", map.keySet().size());
        map.forEach((k, v) -> redisUtil.set(k, v));

    }

    Map<String, QuotationInfoEntity> analysisData(String data) {
        Object eval = JSONPath.eval(String.valueOf(data), "$.data.diff");
        HashMap<String, QuotationInfoEntity> objectMap = JSON.parseObject(String.valueOf(eval), new TypeReference<>() {
        });
        return objectMap.values().stream().collect(Collectors.toMap(QuotationInfoEntity::getF12, t -> t));
    }

    void checkRules(List<QuotationInfoEntity> lists) {
        List<String> list = Arrays.asList("602288", "600886");
    }

}
